#查找指定文件夹下的所有商品D:\易刊蝦皮助理國際版\AppExt\Cache\html

import os,re,sys,io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
from pyquery import PyQuery as pq #pip install pyquery
#https://jingyan.baidu.com/article/a3761b2bc78d731576f9aaa5.html

# my_path=input(r"请输入要判断的文件夹路径 递归搜索哦 路径应该长成这个样子 D:\易刊蝦皮助理國際版\AppExt\Cache\html：")
# my_path=r"D:\易刊蝦皮助理國際版\AppExt\Cache\html"

#获取淘宝店dsr情况
def getTaobao_dsr(doc,dsr_dict):

    n=1
    for i in doc('.tb-shop-rate dd'):
        # dsr_list.append(i.attrib['class']+' ')
        bianliang='dsr状态%s'%n
        dsr_dict.update({bianliang:i.attrib['class']})
        n+=1
    n = 1
    for i in doc('.tb-shop-rate dd a'):
        a=i.text
        txt=re.findall('\d+.\d+', a)[0]
        dsr_dict.update({'dsr分数%s' % n:txt})
        n+=1

    jinpai=doc('a.tb-gold-icon').attr('title')
    if jinpai is None:
        pass
    else:
        dsr_dict.update({'金牌': jinpai})
    return dsr_dict


#天猫DSR情况
def getTaml_dsr(doc,dsr_dict):
    n = 1
    for i in doc('#shopExtra .main-info s'):
        dsr_dict.update({'dsr状态%s'%n: i.attrib['class']})
        n+=1
    n = 1
    for i in doc('#shopExtra .main-info span'):

        dsr_dict.update({'dsr分数%s'%n: i.text})
        n+=1
    return dsr_dict

#判断是否包含新品标签
def newof(doc,path,HtmlConthtml,dsr_dict):
    #判断是淘宝还是天猫
    if "tmall.com" in doc('title').text():
        if "“天猫新品”是指天猫首次上架的特定商品" in HtmlConthtml:
            print(path+"\t有新品标签")
            新品标签 = '有'
            dsr=getTaml_dsr(doc,dsr_dict)
            # txt_list.append(path + "\t有\t"+re.findall("\d{9,15}",path)[0]+'\t'+re.findall('<link rel="canonical" href="(.*)">',HtmlConthtml)[0]+'\t'+getTaml_dsr(doc))
        else:
            print(path + "\t无新品标签")
            新品标签='无'
            dsr=getTaml_dsr(doc,dsr_dict)
            # txt_list.append(path + "\t无\t"+re.findall("\d{9,15}",path)[0]+'\t'+re.findall('<link rel="canonical" href="(.*)">',HtmlConthtml)[0]+'\t'+getTaml_dsr(doc))
        my_dict = {'月销量': doc('[data-label="月销量"] .tm-count').text(),
                       '店铺名': doc('.slogo-shopname strong').text(),
                       '快递': doc('#J_PostageToggleCont span').text(),
                       'path': path,
                       '有新品标签': 新品标签,
                       'ID': re.findall("\d{9,15}", path)[0],
                       '链接': re.findall('<link rel="canonical" href="(.*)">', HtmlConthtml)[0], '描述': dsr}
    elif '-淘宝网'in doc('title').text():
        if '新品' in doc('#J_tbExtra').text():
            print(path+"\t有新品标签")
            dsr=getTaobao_dsr(doc,dsr_dict)
            新品标签 = '有'
            # txt_list.append(path + "\t有\t"+re.findall("\d{9,15}",path)[0]+'\t'+re.findall('<link rel="canonical" href="(.*)">',HtmlConthtml)[0]+'\t'+getTaml_dsr(doc))
        else:
            print(path + "\t无新品标签")
            新品标签 = '无'
            dsr=getTaobao_dsr(doc,dsr_dict)
        my_dict={'月销量': doc('#J_RateCounter').text(),
                 '店铺名': doc('.tb-shop-name a').attr('title'),
                 '快递': doc('span:contains(快递)').text(),
                 'path':path,
                 '有新品标签':新品标签,
                 'ID':re.findall("\d{9,15}",path)[0],
                 '链接':re.findall('<link rel="canonical" href="(.*)">',HtmlConthtml)[0],
                 '描述':dsr}
        if my_dict['店铺名']==' ':
            my_dict.update({'店铺名':doc('.shop-name-link').text()})

    txt_list.append(my_dict)


#创建存储txt的列表
txt_list=[]

def bianli(rootDir):
    for root,dirs,files in os.walk(rootDir):
        for file in files:
            dsr_dict = {'店铺名': ' ', '月销量': ' ', 'dsr状态1': '', 'dsr状态2': '', 'dsr状态3': '', 'dsr分数1': '',
                        'dsr分数2': '', 'dsr分数3': '', '快递': '0', '金牌': ' '}
            path=os.path.join(root,file)
            if "item.htm" in os.path.join(root,file):
                # print("是item文件  "+path)
                Html = open(path, 'r', encoding='utf-8')
                HtmlConthtml = Html.read()
                doc = pq(HtmlConthtml)
                newof(doc,path,HtmlConthtml,dsr_dict)
        for dir in dirs:
            bianli(dir)


def writetxt(txt_list):
    filename = '../../文本输出.txt'
    with open(filename,'w',encoding='utf-8') as f: # 如果filename不存在会自动创建， 'w'表示写数据，写之前会清空文件中的原有数据！
        f.write("店铺名\t月销量\t路径\t有新品标签\tID\t链接\tdsr状态1\tdsr状态2\tdsr状态3\tdsr分数1\tdsr分数2\tdsr分数3\t快递费\t金牌\n")
        for i in txt_list:
            月销量='0'
            if i['描述']['快递']=='':
                kuaidifei='0'
            else:
                try:
                    kuaidifei=str(re.findall('\d+.\d+',i['快递'])[0])
                except:
                    kuaidifei='0'
                try:
                    月销量 = re.findall('\d+', str(i['月销量']))[0]
                    if 月销量 =='' or 月销量 is None:
                        月销量 = '无数据'
                except:
                    月销量='无数据'
                try:
                    店铺名 = i['店铺名']
                    if 店铺名=='' or 店铺名 is None:
                        店铺名='无数据'
                except:
                    店铺名 = '无数据'
            try:
                data=店铺名+'\t'+str(月销量)+'\t'+i['path']+'\t'+i['有新品标签']+'\t'+str(i['ID'])+'\t'+i['链接']+'\t'+i['描述']['dsr状态1']+'\t'+i['描述']['dsr状态2']+'\t'+i['描述']['dsr状态3']+'\t'+str(i['描述']['dsr分数1'])+'\t'+str(i['描述']['dsr分数2'])+'\t'+str(i['描述']['dsr分数3'])+'\t'+kuaidifei+'\t'+i['描述']['金牌']
                print('开始写入%s' % data)
            except:
                print('遇到异常')
            try:
                f.write(data+'\n')
            except:
                sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gbk')
                try:
                    f.write(data+'\n')
                except:
                    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
                    try:
                        f.write(data+'\n')
                    except:
                        print('编码错误 无法写入，准备跳过此条记录...  %s'%i)
                        continue
    print("文本输出路径为  %s"%(os.path.dirname(__file__)+'/%s'%filename).replace('/','\\'))


rootDir='D:\易刊蝦皮助理國際版\AppExt\Cache\html'
# rootDir=r'D:\易刊蝦皮助理國際版\AppExt\Cache\新建文件夹'
bianli((rootDir))
writetxt(txt_list)